#ifndef __EJ_BSP_SPI_H
#define __EJ_BSP_SPI_H

#include "common_def.h"

/*-------------------------------- Type Definitions -----------------------------*/
typedef enum {
    SPI_OK,                 ///< Operation succeeded
    SPI_ERR_TIMEOUT,        ///< Communication timeout
    SPI_ERR_INVALID_CFG,    ///< Invalid configuration (e.g., NULL pointers)
    SPI_ERR_BUS_NOT_FOUND,  ///< Specified bus name not registered
    SPI_ERR_DEVICE_EXISTS,  ///< Device name already registered
    SPI_ERR_BUS_NOT_INIT,   ///< Bus not initialized
    SPI_ERR_DEVICE_NOT_FOUND,
    SPI_ERR_NOT_PARAM,
} spi_status_e;

typedef enum {
    SPI_MODE_SW,    ///< Software (bit-banged) SPI
    SPI_MODE_HW     ///< Hardware peripheral-based SPI
} spi_mode_e;

// SPI工作模式 (CPOL/CPHA)
typedef enum {
    SPI_MODE0 = 0,  ///< CPOL=0, CPHA=0
    SPI_MODE1,      ///< CPOL=0, CPHA=1
    SPI_MODE2,      ///< CPOL=1, CPHA=0
    SPI_MODE3       ///< CPOL=1, CPHA=1
} spi_work_mode_e;

// 数据位顺序
typedef enum {
    SPI_MSB_FIRST,  ///< 高位在前
    SPI_LSB_FIRST   ///< 低位在前
} spi_bit_order_e;

// 软件SPI配置结构
typedef struct {
    // GPIO控制函数
    void (*sck_set)(uint8_t val);     ///< 设置SCK引脚电平
    void (*mosi_set)(uint8_t val);    ///< 设置MOSI引脚电平
    uint8_t (*miso_get)(void);        ///< 读取MISO引脚电平
    void (*cs_set)(uint8_t val);      ///< 设置片选引脚电平
    
    // 时序控制
    void (*delay_us)(uint16_t us);    ///< 微秒延时函数
    uint16_t clock_delay;             ///< 时钟脉冲宽度(微秒)
    
    // SPI参数
    spi_work_mode_e mode;             ///< SPI工作模式
    spi_bit_order_e bit_order;        ///< 数据位顺序
} sw_spi_cfg_t;

// 硬件SPI操作函数集
typedef struct {
    void (*init)(void);  ///< 初始化硬件
    void (*cs_set)(uint8_t cs_pin, uint8_t state); ///< 设置片选引脚状态
    spi_status_e (*transfer)(const uint8_t *tx_data, uint16_t tx_len,
                           uint8_t *rx_data, uint16_t rx_len);
} hw_spi_ops_t;

// 硬件SPI配置
typedef struct {
    hw_spi_ops_t ops;     ///< 硬件操作函数集
} hw_spi_cfg_t;

// SPI驱动信息
typedef struct {
    spi_mode_e mode;      ///< SPI模式(软件/硬件)
    union {
        sw_spi_cfg_t sw; ///< 软件模式配置
        hw_spi_cfg_t hw; ///< 硬件模式配置
    } cfg;
} spi_drv_info_t;

// SPI总线结构
typedef struct {
    const char *bus_name;     ///< 总线名称
    spi_drv_info_t drv_info;  ///< 驱动信息
    bool initialized;         ///< 初始化标志
} spi_bus_t;

// SPI设备结构
typedef struct {
    const char *dev_name;     ///< 设备名称
    const char *bus_name;     ///< 所属总线名称
    uint8_t cs_pin;           ///< 片选引脚编号
} spi_device_t;

/*---------------------------- Public API -----------------------------*/
// 低级驱动API
spi_status_e ej_bsp_spi_register_bus(const char *bus_name, const spi_drv_info_t *drv_info);
spi_status_e ej_bsp_spi_init_bus(const char *bus_name);

// 高级应用API
spi_status_e ej_bsp_spi_attach_device(const char *bus_name, const char *dev_name, uint8_t cs_pin);
spi_status_e ej_bsp_spi_transfer(const char *dev_name, 
                                const uint8_t *tx_data, uint16_t tx_len,
                                uint8_t *rx_data, uint16_t rx_len);

// 扩展API
spi_status_e ej_bsp_spi_write(const char *dev_name, const uint8_t *data, 
                            uint16_t len);
spi_status_e ej_bsp_spi_read(const char *dev_name, uint8_t *data, 
                           uint16_t len);
spi_status_e ej_bsp_spi_reg_write(const char *dev_name, uint8_t reg, 
                                const uint8_t *data, uint16_t len);
spi_status_e ej_bsp_spi_reg_read(const char *dev_name, uint8_t reg, 
                               uint8_t *data, uint16_t len);

#endif // BSP_SPI_H